#!/usr/bin/perl
# SCRIPT PAGE
use strict;
use warnings;
require 'forum_common.pl';

connect_to_database() or bad_deal();
my $user = get_current_user(get_sess_id());

bad_deal('',yaali($user)) if ($user);

record_this_hit();

my $prev = param('http_referer') || referer() || $forum::root_path;
$prev = $forum::root_path if ((split'/',$prev)[-1] eq $0);


my ($msg, $username, $password, $password2, $email) = ('');
if (defined($username = param('username')) &&
	defined($password = param('password')) &&
	defined($password2 = param('password2')) &&
	defined($email = param('email')) &&
	$ENV{'REQUEST_METHOD'} eq 'POST')
{
	my ($hidee,$ip_addr,$hostname,$newuser,$sess_id);
	$hidee = defined($hidee = param('hidee')) ? 1 : 0;
	$ip_addr = $ENV{'REMOTE_ADDR'};
	$hostname = $ENV{'REMOTE_HOST'};
	if ($newuser = create_user($username,$password,$password2,
								$email,$hidee,$ip_addr,$hostname)
		and
		$sess_id = create_sess_id($newuser))
	{
		push @forum::cookies, sess_id_cookie($sess_id);
		good_deal
		(
			div({-class=>'calign'},
				'You have registered as ',
				a({-href=>"member.cgi?id=$newuser->{ID}"},
					$newuser->{USERNM}).'!',
				br(),
				a({-href=>$prev}, 'Go back'))
		);
	}
	else
	{
		$msg = nl2br(forum_error());
	}
}

print
	header($forum::http_header), forum_start(), $msg,
	'<div class="ftable divcalign" style="border-spacing:1em;">',
	start_form(-action=>'', -method=>'post'),
	hidden(-name=>'http_referer', -value=>$prev, -override=>1),
	'<fieldset id="register">',
	legend('Register'),
	div({-class=>'ftr'},
		div({-class=>'ftd ralign'}, 'Username: '),
		div({-class=>'ftd'},
			textfield(-name=>'username', -size=>24, -maxlength=>24))),
	div({-class=>'ftr'},
		div({-class=>'ftd ralign'}, 'Password: '),
		div({-class=>'ftd'},
			password_field(-name=>'password', -size=>24, -maxlength=>24,
							-override=>1))),
	div({-class=>'ftr'},
		div({-class=>'ftd ralign'}, 'Confirm Password: '),
		div({-class=>'ftd'},
			password_field(-name=>'password2',-size=>24,-maxlength=>24,
							-override=>1))),
	div({-class=>'ftr'},
		div({-class=>'ftd ralign'}, 'Email Address: '),
		div({-class=>'ftd'},
			textfield(-name=>'email', -size=>24, -maxlength=>128))),
	div({-class=>'ftr'},
		div({-class=>'ftd ralign'}, 'Hide email?'),
		div({-class=>'ftd'},
			checkbox(-name=>'hidee',-checked=>1,-value=>'1',-label=>''))),
	div({-class=>'ftr'},
		div({-class=>'ftd'}, ''),
		div({-class=>'ftd'}, submit(-name=>'submit', -value=>'Register'))),
	'</fieldset>',
	'</form>', # I hate perl
	'</div>';
# print the "Forgot Password?" form

gameover:
print forum_end();
forum_disconnect();
